package hlhj.fhp.supreme.activitys

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.widget.RelativeLayout
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import fhp.hlhj.giantfold.utils.LogUtil
import hlhj.fhp.supreme.BaseAty
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.javabean.MallBean
import hlhj.fhp.supreme.javabean.TecMaterialMultiBean
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.utils.JsonCallBack
import hlhj.fhp.supreme.utils.MyUtils
import kotlinx.android.synthetic.main.activity_tec_material_aty.*
import java.util.ArrayList

class TecMaterialAty : BaseAty() {
    private val datas= ArrayList<MallBean.DataBeanX>()
    private lateinit var adp: BaseMultiItemQuickAdapter<MallBean.DataBeanX, BaseViewHolder>
    override fun getContentId(): Int {
        return R.layout.activity_tec_material_aty
    }

    override fun initView() {
        //        头部IMG
        val headBean = MallBean.DataBeanX()
        headBean.setType(1)
//        正版书籍

        setAdp()

        getData()
    }

    private fun setAdp() {
        adp = object : BaseMultiItemQuickAdapter<MallBean.DataBeanX, BaseViewHolder>(datas) {
            init {

                addItemType(2, R.layout.read_book)
                addItemType(3, R.layout.read_book)


            }

            override fun convert(helper: BaseViewHolder?, item: MallBean.DataBeanX?) {
                when (helper?.itemViewType) {
                    1 -> {

                    }
                    2 -> {
                        helper?.setText(R.id.tvTittle, item?.name)
                        val recyclerView = helper?.getView<RecyclerView>(R.id.recyclerview)
                        if (item?.data!=null){
                            val adp= object :BaseQuickAdapter<MallBean.DataBeanX.DataBean,BaseViewHolder>(R.layout.tec_item,item?.data){
                                override fun convert(helper: BaseViewHolder?, item: MallBean.DataBeanX.DataBean?) {
                                    Glide.with(mContext).load(item?.img).asBitmap().into(helper?.getView(R.id.itemIcon))
                                    helper?.setText(R.id.itemTittle,item?.title)
                                    helper?.setText(R.id.itemPoint,"￥${item?.money}")
                                    val layout = helper?.getView<RelativeLayout>(R.id.lo)
                                    val params = layout!!.layoutParams as GridLayoutManager.LayoutParams
                                    when (helper?.adapterPosition%3){
                                        0->{
                                            params.setMargins(10,10,0,0)
                                        }
                                        1->{
                                            params.setMargins(10,10,10,0)

                                        }
                                        2->{
                                            params.setMargins(0,10,10,0)
                                        }
                                    }
                                    layout.layoutParams=params
                                }
                            }
                            recyclerView.adapter = adp
                            recyclerView.layoutManager = GridLayoutManager(this@TecMaterialAty, 3)
                            adp.setOnItemClickListener { adapter, view, position ->
                                val intent = Intent(mContext, HtmlAty::class.java)
                                intent.putExtra("id",item?.data[position].id)
                                intent.putExtra("et","0")
                                startActivity(intent)
                            }
                        }

                    }
                    3 -> {
                        val n = item?.name
                        LogUtil.log("报错的标题${n}报错的")
                        helper?.setText(R.id.tvTittle, item?.name)
                        val recyclerView = helper?.getView<RecyclerView>(R.id.recyclerview)
                        if (item?.data!=null){
                            val adp=object :BaseQuickAdapter<MallBean.DataBeanX.DataBean,BaseViewHolder>(R.layout.tec_m_video_item,item?.data){
                                override fun convert(helper: BaseViewHolder?, item: MallBean.DataBeanX.DataBean?) {
                                    Glide.with(mContext).load(item?.img).asBitmap().into(helper?.getView(R.id.itemIcon))
                                    helper?.setText(R.id.itemName,item?.title)
                                    helper?.setText(R.id.itemPrice,item?.money.toString())
                                    val layout = helper?.getView<RelativeLayout>(R.id.lo)
                                    val params = layout!!.layoutParams as GridLayoutManager.LayoutParams
                                    when (helper?.adapterPosition%3){
                                        0->{
                                            params.setMargins(10,10,0,0)
                                        }
                                        1->{
                                            params.setMargins(10,10,10,0)

                                        }
                                        2->{
                                            params.setMargins(0,10,10,0)
                                        }
                                    }
                                    layout.layoutParams=params
                                }
                            }
                            recyclerView.adapter = adp
                            recyclerView.layoutManager = GridLayoutManager(this@TecMaterialAty, 3)
                            adp.setOnItemClickListener { adapter, view, position ->
                                val intent = Intent(this@TecMaterialAty, HtmlAty::class.java)
                                intent.putExtra("id",item?.data[position].id)
                                intent.putExtra("et","0")
                                startActivity(intent)
                            }
                        }

                    }
                }
            }
        }
        recyclerview.adapter = adp
        recyclerview.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
    }

    private fun getData() {
        OkGo.post<MallBean>(Urls.BaseUrl+Urls.POINT_MALL)
                .params("type",0)
                .execute(object :JsonCallBack<MallBean>(this){
                    override fun onSuccess(response: Response<MallBean>?) {
                        val bean = response?.body()
                        if (bean?.code==1){
                            for (i in 0..bean?.data.size-1){
                                if (i==0){
                                    bean?.data[i].setType(2)

                                }else{
                                    bean?.data[i].setType(3)
                                }
                            }
                            datas.addAll(bean?.data)
                            adp.notifyDataSetChanged()
//                            setAdp()
                        }else{
                            MyUtils.toast(bean?.msg)
                        }
                    }
                })
    }

    override fun initListener() {
        btExit.setOnClickListener { finish()}
    }
}
